<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    width="740px"
    :visible="visible"
    :append-to-body="true"
    :close-on-click-modal="false"
    custom-class="ele-dialog-form"
    :title="isUpdate ? '修改优惠券' : '添加优惠券'"
    @update:visible="updateVisible"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="120px">
      <el-form-item label="消费券类型:" prop="consumer_type">
        <el-radio-group v-model="form.consumer_type">
          <el-radio :label="1" value="1">全部</el-radio>
          <el-radio :label="2" value="2">线上核销</el-radio>
          <el-radio :label="3" value="3">独立核销</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="价格:" prop="price">
        <el-input clearable v-model="form.price" placeholder="请输入价格" />
      </el-form-item>

      <el-form-item label="消费券类型:" prop="issue_type">
        <el-radio-group v-model="form.issue_type">
          <el-radio :label="1" value="1">固定金额</el-radio>
          <el-radio :label="2" value="2">比例折扣</el-radio>
          <el-radio :label="3" value="3">实物券</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item label="实物名称:" prop="title" v-if="form.issue_type == 3">
        <el-input
          clearable
          v-model="form.title"
          placeholder="请输入消费券满足金额"
        />
      </el-form-item>

      <el-form-item label="图片:" prop="goods_img" v-if="form.issue_type == 3">
        <span slot="label">
          图片

          <el-tooltip placement="top">
            <div slot="content"> 建议上传 710 * 300尺寸,或者等比例图片 </div>
            <i class="el-icon-question" />
          </el-tooltip>
        </span>
        <!--<ele-image-upload v-model="img" :limit="1" :drag="true" :multiple="false"  @upload="onUpload" /> -->
        <div class="ele-image-upload-list">
          <div
            class="ele-image-upload-item ele-image-upload-button"
            @click="modalPicTap('dan', 'goods_img', '图片')"
          >
            <div>
              <div tabindex="0" class="el-upload el-upload--text">
                <div class="el-upload-dragger">
                  <i class="el-icon-plus ele-image-upload-icon"></i>
                </div>
                <div
                  class="ele-image-upload-item"
                  style="margin: 0 0 0 0"
                  v-if="form.goods_img != ''"
                >
                  <div class="el-image">
                    <img
                      :src="form.goods_img"
                      width="100%"
                      height="100%"
                      class="el-image__inner"
                      style="object-fit: cover"
                    />
                  </div>
                  <div
                    class="ele-image-upload-close"
                    @click="handleRemove('goods_img')"
                    ><i class="el-icon-close"></i
                  ></div>
                </div>
              </div>
            </div>
          </div>
        </div>
      </el-form-item>

      <el-form-item
        label="消费券满足金额:"
        prop="satisfy_amount"
        v-if="form.issue_type == 1 || form.issue_type == 2"
      >
        <el-input
          clearable
          v-model="form.satisfy_amount"
          placeholder="请输入消费券满足金额"
        />
      </el-form-item>

      <el-form-item
        label="抵扣金额:"
        prop="deductible_amount"
        v-if="form.issue_type == 1"
      >
        <el-input
          clearable
          v-model="form.deductible_amount"
          placeholder="请输入消费券抵扣金额"
        />
      </el-form-item>

      <el-form-item
        label="折扣比例:"
        prop="deductible_amount"
        v-if="form.issue_type == 2"
      >

          <el-input v-model="form.deductible_amount" clearable>
                  <template slot="append">%</template>
                </el-input>

       

        <div class="text-info">支持0-100</div>
      </el-form-item>

      <el-form-item label="有效时间:" prop="expire_time">
        <el-date-picker
          v-model="form.expire_time"
          type="datetime"
          value-format="yyyy-MM-dd HH:mm"
          placeholder="选择日期"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="商家比例:" prop="store_undertake_proportion">

          <el-input v-model="form.store_undertake_proportion" clearable>
                  <template slot="append">%</template>
                </el-input>
      
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="updateVisible(false)">取消 </el-button>
      <el-button type="primary" :loading="loading" @click="save"
        >保存
      </el-button>
    </template>
    <uploadPictures
      :isChoice="isChoice"
      :visible.sync="modalPic"
      @getPic="getPic"
      :gridBtn="gridBtn"
      :gridPic="gridPic"
      :title="modalTitle"
    ></uploadPictures>
  </ele-modal>
</template>

<script>
import uploadPictures from '@/components/uploadPictures';
import EleImageUpload from 'ele-admin/es/ele-image-upload';
import PagesSearch from '@/views/common/pages/pages-search';
import { add, update } from '@/api/consumer';
const DEFAULT_FORM = {
  id: 0,
  name: '',
  password: '',
  telphone: '',
  url: '',
  expire_time: '',
  status: 1,
  consumer_type: 1,
  price: 0,
  deductible_type: 1,
  goods_img: [],
  title: '',
  issue_type: '',
  deductible_amount: '',
  satisfy_amount: '',
  store_undertake_proportion: ''
};

export default {
  name: 'OpenEdit',
  components: { EleImageUpload, uploadPictures, PagesSearch },
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  data() {
    return {
      modalTitle: '选择图片',
      modalPic: false,
      isChoice: '单选',
      gridBtn: {
        xl: 4,
        lg: 8,
        md: 8,
        sm: 8,
        xs: 8
      },
      gridPic: {
        xl: 6,
        lg: 8,
        md: 12,
        sm: 12,
        xs: 12
      },
      // 表单数据

      form: { ...DEFAULT_FORM },
      obj: {},
      // 表单验证规则
      rules: {},
      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false,
      num: 1
    };
  },
  methods: {
    updateCount(obj) {
      this.obj = obj;
    },
    // 选择图片
    modalPicTap(tit, picTit, openTitle) {
      this.modalTitle = openTitle;
      this.isChoice = tit === 'dan' ? '单选' : '多选';
      this.picTit = picTit;
      this.modalPic = true;
    },
    // 选中图片
    getPic(pc) {
      switch (this.picTit) {
        case 'goods_img':
          this.form.goods_img = pc.satt_dir;
          break;
        case 'background':
          this.form.background = pc.satt_dir;
          break;
      }
      this.modalPic = false;
    },
    //删除图片
    handleRemove(field) {
      this.form[field] = '';
    },
    handleChange(value) {},
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (!valid) {
          return false;
        }

        this.loading = true;
        const data = {
          ...this.form
        };

        const saveOrUpdata = this.isUpdate ? update : add;

        saveOrUpdata(data)
          .then((msg) => {
            this.loading = false;
            this.$message.success(msg);
            this.updateVisible(false);
            this.$emit('done');
          })
          .catch((e) => {
            this.loading = false;
            this.$message.error(e.message);
          });
      });
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  },
  watch: {
    visible(visible) {
      if (visible) {
        if (this.data) {
          this.$util.assignObject(this.form, {
            ...this.data
          });
          this.isUpdate = true;
        } else {
          this.isUpdate = false;
        }
      } else {
        this.$refs['form'].clearValidate();
        this.form = { ...DEFAULT_FORM };
      }
    }
  }
};
</script>
<style lang="less" scoped>
/deep/ .el-divider--horizontal {
  margin: 0 0 12px 0;
}

.total {
  font-size: 14px;
  font-weight: 400;
  color: #999;
  line-height: 22px;
}
.ivu-text-right {
  text-align: right;
}
.analysis-chart-card-num {
  font-size: 30px;
}

.analysis-chart-card-content {
  height: 40px;
  box-sizing: border-box;
  margin-bottom: 12px;
}

.analysis-chart-card-text {
  padding-top: 12px;
}
.container {
  /* 矩形 6529 */
  padding: 20px;
  height: 61px;
  border-radius: 10px;
  background: rgba(255, 248, 248, 1);
  color: rgba(255, 78, 78, 1);
  font-weight: 700;
  margin-top: 30px;
}
</style>
